﻿@using WebApp.BLL;
@model WebApp.Models.S_user

@{
    Layout = "~/Views/Shared/_DialogLayout.cshtml";

}

@using (Html.BeginForm("", "", FormMethod.Post, new { @class = "form-horizontal form-inline", id = "inputform" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="dialog-content">
        <table cellpadding="3" cellspacing="0" border="0" class="inputform">
            <tr>
                <td class="td-label">用户名</td>
                <td class="td-control">
                    @Html.TextBox("Username", @Model.Username, new { @class = "form-control datafield", @readonly = "true", @data_fieldname = "Username" })
                </td>
            </tr>
            <tr>
                <td class="td-label">旧密码</td>
                <td class="td-control">
                    @Html.Password("OldPassword", "", new { @class = "form-control datafield", @data_fieldname = "Password" })
                </td>
            </tr>
            <tr>
                <td class="td-label">新密码</td>
                <td class="td-control">
                    @Html.Password("NewPassword", "", new { @class = "form-control datafield", @data_fieldname = "Password" })
                </td>
            </tr>
            <tr>
                <td class="td-label">再输一次</td>
                <td class="td-control">
                    @Html.Password("ConfrimPassword", "", new { @class = "form-control datafield", @data_fieldname = "Password" })
                </td>
            </tr>
        </table>
    </div>
    <div class="dialog-footer">
        <button id="btncancel" class="btn btn-small btn-default">取消</button>
        <button id="btnsave" class="btn btn-small btn-primary">确认</button>
    </div>
}

@section scripts{
    <script type="text/javascript">
        //初始化表单验证
        function initValidate() {
            //表单验证
            $('#inputform').validate({
                rules: {
                    OldPassword:
                    {
                        required: true,
                        remote: {
                            url: baseurl + 'Manage/User/PasswordChk',
                            type: 'post',
                            data: {
                                oldPassword: $("#OldPassword").val(),
                            },
                            dataFilter: function (data, type) {
                                if (data == "ok")
                                    return true;
                                else
                                    return false;
                            }
                        }
                    },
                    NewPassword: {
                        required: true,
                        //remote: {
                        //    url: baseurl + 'Manage/User/PasswordLengthChk',
                        //    type: 'post',
                        //    data: {
                        //        newPassword: $("#NewPassword").val(),
                        //    },
                        //    dataFilter: function (data, type) {
                        //        if (data == "yes")
                        //            return true;
                        //        else
                        //            return false;
                        //    }
                        //}
                    },
                    ConfrimPassword:
                        {
                            required: true,
                            equalTo: "#NewPassword"
                        }
                },
                errorPlacement: function (error, element) {
                    error.appendTo(element.parent());
                },
                messages: {
                    OldPassword: {
                        remote: "输入的密码不正确！"
                    },
                    NewPassword: {
                        remote: "您的密码必须是6~16位的字符，且必须是数字和字母的组合。可以使用特殊符号，字母区分大小写！"
                    },
                    ConfrimPassword: {
                        equalTo: '两次密码输入不一致！'
                    }
                }
            });
        }

        $(document).ready(function () {
            initValidate();

            //取消
            $("#btncancel").click(function (evt) { evt.preventDefault(); parent.closeChangepwd(); return false; });

            $("#btnsave").click(function (evt) {
                evt.preventDefault();

                if (!$("#inputform").valid()) {
                    return;
                }

                var $btn = $(this);

                $btn.attr("disabled", true);

                $.ajax({
                    type: "post",
                    url: baseurl + "Manage/User/SavePassword",
                    data: {
                        pwd: $("#NewPassword").val()
                    },
                    success: function (e) {
                        $btn.attr("disabled", false);

                        if (e.Ret == "0") {
                            $.messager.alert("消息", "密码修改成功！", "info", function () {
                                $("#btncancel").click();
                            });
                        } else {
                            $.messager.alert("错误", e.Msg, "error");
                        }
                    },
                    error: function (xhr) {
                        $btn.attr("disabled", false);
                        showError(xhr);
                    }
                });
            });
        });
    </script>
}

